

<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h1 class="title">欢迎登录</h1>
      </div>
      
      <div class="login-content">
        <div class="login-left">
          <div class="left-content">
            <h2>欢迎使用系统</h2>
            <p>简约、高效、安全的用户管理平台</p>
            <div class="features">
              <div class="feature-item">
                <i class="el-icon-lock"></i>
                <span>安全可靠</span>
              </div>
              <div class="feature-item">
                <i class="el-icon-data-line"></i>
                <span>高效管理</span>
              </div>
              <div class="feature-item">
                <i class="el-icon-user"></i>
                <span>专业服务</span>
              </div>
            </div>
          </div>
        </div>
        <el-card class="login-card" :body-style="{ padding: '30px' }">
          <el-form :model="userInfo" ref="loginForm" :rules="rules" class="login-form">
            <h3 class="form-title">欢迎登录</h3>
            <el-form-item prop="userName" class="form-item">
              <el-input 
                v-model="userInfo.userName" 
                placeholder="请输入用户名"
                prefix-icon="el-icon-user"
                class="custom-input">
              </el-input>
            </el-form-item>
            <el-form-item prop="userPassword" class="form-item">
              <el-input 
                v-model="userInfo.userPassword" 
                placeholder="请输入密码" 
                type="password"
                prefix-icon="el-icon-lock"
                class="custom-input">
              </el-input>
            </el-form-item>
            <div class="remember-forgot">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <a href="javascript:void(0)" class="forgot-link">忘记密码?</a>
            </div>
            
            <div class="button-group">
              <el-button type="primary" @click="login" class="login-btn">登录</el-button>
              <el-button @click="register" class="register-btn">注册</el-button>
              <el-button @click="resetForm" class="reset-btn">重置</el-button>
            </div>
          </el-form>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import axios  from "../utils/axios.js";
import {ElMessage} from "element-plus";
import router from "../router/index.js";
import { ref, onMounted, onUnmounted } from 'vue';


// 键盘按下时触发的事件
const handleKeyDown = (event) => {
  const keyName = event.key; // 获取按下的键名
  console.log('Key pressed:', keyName)
  if (keyName === 'Enter'){
    login()
  }
};

// 在组件挂载后，添加键盘事件监听
onMounted(() => {
  window.addEventListener('keydown', handleKeyDown);
});

// 在组件卸载时，移除键盘事件监听
onUnmounted(() => {
  window.removeEventListener('keydown', handleKeyDown);
});


const loginForm = ref(null)
const userInfo = ref({
  userName:'',
  userPassword:'',
})

const rememberMe = ref(false)

//rules
// 表单验证规则
const rules = ref({
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '用户名长度在 3 到 10 个字符之间', trigger: 'blur' }
  ],
  userPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 12, message: '密码长度在 6 到 12 个字符之间', trigger: 'blur' }
  ]
});


//method
const resetForm=()=>{
  loginForm.value.resetFields()
}
const login = ()=>{
  loginForm.value.validate((valid) => {
    console.log(valid)
    if (valid) {
      axios.post('/login', JSON.stringify(userInfo.value))
          .then((res) => {
            console.log(res)
            if  (res.code==2000){
              ElMessage.success("登录成功！")
              router.push('/user')
            }else {
              ElMessage.error("登录失败！")
            }
          })
    } else {
      console.log('表单验证失败');
      return false;
    }
  })
}

const register = ()=>{
  loginForm.value.validate((valid) => {
    if (valid) {
      axios.post('/register', JSON.stringify(userInfo.value))
          .then((res) => {
            if (res.code==2000){
              ElMessage.success("注册成功！")
            }else {
              ElMessage.error("注册失败！")
            }
          })
    } else {
      console.log('表单验证失败');
      return false;
    }
  })
}

</script>

<style scoped>
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  background-image: url('/bg.svg');
  background-size: cover;
  background-position: center;
  padding: 0;
  margin: 0;
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  height: 100vh;
  max-width: 100%;
  padding: 0 20px;
  animation: fadeIn 0.8s ease-in-out;
}

.login-header {
  text-align: center;
  margin-bottom: 25px;
}

.title {
  color: #2c3e50;
  font-size: 28px;
  margin-bottom: 8px;
  font-weight: 600;
}

.subtitle {
  color: #7f8c8d;
  font-size: 16px;
}

.login-content {
  display: flex;
  width: 100%;
  justify-content: center;
  align-items: center;
  gap: 40px;
  padding: 0 5%;
}

.login-left {
  flex: 1;
  max-width: 500px;
  color: #2c3e50;
  text-align: left;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
  height: 420px;
}

.left-content {
  width: 100%;
}

.login-left h2 {
  font-size: 36px;
  margin-bottom: 16px;
  font-weight: 600;
  color: #1a1a1a;
}

.login-left p {
  font-size: 18px;
  color: #4a4a4a;
  line-height: 1.6;
  margin-bottom: 30px;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-top: 30px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 15px;
}

.feature-item i {
  font-size: 24px;
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
  padding: 10px;
  border-radius: 50%;
}

.feature-item span {
  font-size: 16px;
  font-weight: 500;
  color: #2c3e50;
}

.login-card {
  flex: 1;
  max-width: 420px;
  border-radius: 16px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  background: white;
  height: 420px;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.login-form {
  width: 100%;
}

.form-title {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 25px;
  text-align: center;
}

.form-item {
  margin-bottom: 22px;
}

.custom-input {
  border-radius: 8px;
  height: 50px;
  transition: all 0.3s;
  font-size: 16px;
}

.custom-input:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 14px;
}

.forgot-link {
  color: #409eff;
  text-decoration: none;
  transition: color 0.3s;
}

.forgot-link:hover {
  color: #66b1ff;
  text-decoration: underline;
}

.button-group {
  display: flex;
  gap: 15px;
  margin-top: 20px;
}

.login-btn, .register-btn, .reset-btn {
  flex: 1;
  height: 50px;
  border-radius: 10px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s;
  font-size: 16px;
}

.login-btn {
  background: #409eff;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-btn:hover {
  background: #66b1ff;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
}

.register-btn:hover, .reset-btn:hover {
  transform: translateY(-2px);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .login-container {
    max-width: 100%;
    padding: 20px;
  }
  
  .login-content {
    flex-direction: column;
    gap: 30px;
    padding: 0 5%;
  }
  
  .login-left {
    max-width: 100%;
    text-align: center;
    margin-bottom: 20px;
    padding: 30px 20px;
    height: auto;
  }
  
  .login-card {
    max-width: 100%;
    height: auto;
  }
  
  .features {
    align-items: center;
  }
  
  .button-group {
    flex-direction: column;
  }
  
  .login-btn, .register-btn, .reset-btn {
    width: 100%;
    margin-bottom: 10px;
  }
}
</style>
